﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Demo3Layer_Payment.Data_Tranfer_Object;
namespace Demo3Layer_Payment.Data_Access_Layer
{
    class CustomersDAO
    {
        #region"Attribute"
        private DataProvider objData;
        private CustomersDTO objCustomersDTO;
        string strError;
        #endregion

        #region "Property"
        /// <summary>
        /// Property of attribute objCustomerDTO
        /// </summary>
        public CustomersDTO ObjCustomersDTO
        {
            get { return objCustomersDTO; }
            set { objCustomersDTO = value; }
        }
        #endregion

        #region"Constructor method"
        /// <summary>
        /// Constructor method object CustomersDAO' class
        /// </summary>
        public CustomersDAO()
        {
            this.objData = new DataProvider();
            this.strError = string.Empty;
            objCustomersDTO = new CustomersDTO();
        }
        #endregion

        #region"Method"
        /// <summary>
        /// Method Insert new customers
        /// </summary>
        /// <param name="objCustomers"></param>
        /// <returns></returns>
        public bool InsertCustomers()
        {
            if (!objData.IsConnect())
                objData.Connect();
            if (!objData.Connect())
                return false;
            try
            {
                objData.CreateStoreProcedure("CustomersInsert");
                objData.AddParameter("@CustomersID", this.objCustomersDTO.StrCustomersID);
                objData.AddParameter("@CustomersName", this.objCustomersDTO.StrCustomersName);
                objData.AddParameter("@CustomersBirthday", this.objCustomersDTO.DateCustomersBirthday);
                objData.AddParameter("@CustomersPhone", this.objCustomersDTO.StrCustomersPhone);
                objData.AddParameter("@CustomersEmail", this.objCustomersDTO.StrCustomersEmail);
                objData.AddParameter("@CustomersAddress", this.objCustomersDTO.StrCustomersAddress);
                if (objData.ExecuteStoreProcedureNonQuery() <= 0)
                    return false;
            }
            catch (Exception ex)
            {
                this.strError = ex.Message;
                return false;
            }
            finally
            {
                objData.DeConnect();
            }
            return true;
        }
        /// <summary>
        /// Method select all customers
        /// </summary>
        /// <returns></returns>
        public List<CustomersDTO> _SelectAllCustomers()
        {
            List<CustomersDTO> listCustomersDTO = new List<CustomersDTO>();
            DataTable dt = new DataTable();
            if (!objData.IsConnect())
                objData.Connect();
            try
            {
                objData.CreateStoreProcedure("CustomersGetAll");
                dt=objData.ExecuteStoreProcdureToDataTable();
                foreach(DataRow row in dt.Rows)
                {
                    objCustomersDTO = new CustomersDTO();
                    this.objCustomersDTO.StrCustomersID = row["CustomersID"].ToString().Trim();
                    this.objCustomersDTO.StrCustomersName = row["CustomersName"].ToString().Trim();
                    this.objCustomersDTO.DateCustomersBirthday =DateTime.Parse(row["CustomersBirthday"].ToString().Trim());
                    this.objCustomersDTO.StrCustomersPhone = row["CustomersPhone"].ToString().Trim();
                    this.objCustomersDTO.StrCustomersEmail = row["CustomersEmail"].ToString().Trim();
                    this.objCustomersDTO.StrCustomersAddress = row["CustomersAddress"].ToString().Trim();
                    listCustomersDTO.Add(this.objCustomersDTO);
                }
            }
            catch (Exception ex)
            {
                this.strError = ex.Message;
            }
            finally
            {
                objData.DeConnect();
            }
            return listCustomersDTO;
        }

        public DataTable SelectAllCustomers()
        {
            DataTable dt = new DataTable();
            if (!objData.IsConnect())
                objData.Connect();
            try
            {
                objData.CreateStoreProcedure("CustomersGetAll");
                dt = objData.ExecuteStoreProcdureToDataTable();
            }
            catch (Exception ex)
            {
                this.strError = ex.Message;
            }
            finally
            {
                objData.DeConnect();
            }
            return dt;
        }
        #endregion
    }
}
